/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    struct ListNode* head = NULL,*tail = NULL;
    if(list1==NULL&&list2==NULL)
    {
        return NULL;
    }
    if(list1==NULL&&list2)
    {
        return list2;
    }
    if(list2==NULL&&list1)
    {
        return list1;
    }
    while(list1&&list2)
    {
       if(list1->val<list2->val)
       {
           if(head == NULL)
           {
               head = tail = list1;
               list1= list1->next;
           }
           else{
              tail->next = list1;
              tail = tail->next;
              list1 = list1->next;
           }
       }
       else
       {
           if(head == NULL)
           {
               head = tail = list2;
               list2 = list2->next;
           }
           else{
               tail->next = list2;
              tail = tail->next;
              list2 = list2->next;
           }
       }
    }
   if(list1==NULL)
   {
       tail->next = list2;
       tail = tail->next;
   }
   if(list2==NULL)
   {
       tail->next = list1;
       tail = tail->next;
   }
   return head;
}